package beans;

import java.util.ArrayList;
import java.util.List;

import utils.ADFUtil;
import utils.JSFUtil;

public class BackingBean {
    private List<PurchaseRequisition> purchaseReq;
    private int purchaseCount=0;
    private List<Configuration> configList;
    private PurchaseRequisition pr ;
    
    public BackingBean() {
        
        pr =  new PurchaseRequisition();
        TestBean test = new TestBean();
        purchaseReq = new ArrayList<PurchaseRequisition>();
        purchaseReq.add(pr);
        purchaseCount++;
    }
   


    public void setPurchaseReq(List<PurchaseRequisition> purchaseReq) {
        this.purchaseReq = purchaseReq;
    }

    public List<PurchaseRequisition> getPurchaseReq() {
        return purchaseReq;
    }

    public void setPurchaseCount(int purchaseCount) {
        this.purchaseCount = purchaseCount;
    }

    public int getPurchaseCount() {
        return purchaseCount;
    }
    public String addConfig() {
        Object uuid =
            ADFUtil.resolveExpression("#{pageFlowScope.pRequestAdd}");
        Configuration configObject = new Configuration();
        configObject.setParentUuid(uuid.toString());
        for(PurchaseRequisition p : purchaseReq){
            if(uuid.equals(p.getUuid())){
                p.getConfig().add(configObject);
                p.setConfigCount(p.getConfigCount()+1);
            }
        }
       
        return null;
    }
    
    public String deleteConfig() {
        Object parentUuid =
            ADFUtil.resolveExpression("#{pageFlowScope.pParentRemove}");
        Object uuid =
            ADFUtil.resolveExpression("#{pageFlowScope.pConfigRemove}");
        if (parentUuid != null) {
            
                for (PurchaseRequisition p : purchaseReq) {
                    if (parentUuid.equals(p.getUuid())) {
                        for(Configuration c :p.getConfig()){
                            if(c.getUuid().equals(uuid)){
                                p.getConfig().remove(c);
                                return null;
                            }
                        }
                        
                        return null;
                    }
              
            }
        }
        return null;
    }

    public String deleteRequest() {
        Object uuid =
            ADFUtil.resolveExpression("#{pageFlowScope.pRequestRemove}");
        if (uuid != null) {
         
                for (PurchaseRequisition pr : purchaseReq) {
                    if (uuid.equals(pr.getUuid())) {
                        purchaseReq.remove(pr);
                        return null;
                    }
                }
           
        }
        return null;
    }

    public String addRequest() {
    
        PurchaseRequisition pr = new PurchaseRequisition();
        purchaseReq.add(pr);
        purchaseCount++;
        return null;
    }

    public void setConfigList(List<Configuration> configList) {
        this.configList = configList;
    }

    public List<Configuration> getConfigList() {
        Object uuid =
            ADFUtil.resolveExpression("#{requestScope.pRequestRemove}");
        System.out.println("PageFlow-UUID =" +uuid.toString());
        List<Configuration> list = new ArrayList<Configuration>();
        for(PurchaseRequisition c : purchaseReq) {
            System.out.println("List UUID ="+ c.getUuid());
            if (uuid.equals(c.getUuid())) {
                list = c.getConfig();
            }
        }
      return list;
    }
}
